package com.ruoyi.OutboundManagement.controller;

import com.ruoyi.OutboundManagement.domain.DeliveryOperation;
import com.ruoyi.OutboundManagement.domain.OtherOutbound;
import com.ruoyi.OutboundManagement.domain.vo.WarningVO;
import com.ruoyi.OutboundManagement.service.IDeliveryOperationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 出库操作Controller
 *
 * @author admin
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/OutboundManagement/deliveryOperation")
public class DeliveryOperationController extends BaseController {
    @Autowired
    private IDeliveryOperationService deliveryOperationService;


    /**
     * 新增入库操作
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:add')")
    @Log(title = "入库操作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryOperation deliveryOperation) {
        if (deliveryOperation.getStorageStatus().equals("2")) {
            List<Long> materialIds = new ArrayList<>();
            materialIds.add(deliveryOperation.getMaterialId());
            List<DeliveryOperation> deliveryOperationList = deliveryOperationService.selectDeliveryOperationList(materialIds, null);
            if (CollectionUtils.isNotEmpty(deliveryOperationList)) {

                if (null != deliveryOperationList.get(0).getInventoryDifference()) {
                    Long inventoryDifference = deliveryOperationList.get(0).getInventoryDifference();
                    Long shipmentQuantity = Long.valueOf(deliveryOperation.getShipmentQuantity());
                    if (inventoryDifference < 1 || inventoryDifference - shipmentQuantity < 0) {
                        return AjaxResult.error("库存不足");
                    }
                }
            }
        }

        return toAjax(deliveryOperationService.insertDeliveryOperation(deliveryOperation));
    }




//    /**
//     * 查询出入库操作列表
//     */
//    @GetMapping("/list")
//    public TableDataInfo list(DeliveryOperation deliveryOperation) {
//        startPage();
//        List<DeliveryOperation> list = deliveryOperationService.selectDeliveryOperationList(deliveryOperation);
//        return getDataTable(list);
//    }


    /**
     * 库内查询
     */

    @GetMapping("/list")
    public TableDataInfo  list(@RequestParam(value = "materialIds", required = false) List<Long> materialIds,String materialName) {
        startPage();
        List<DeliveryOperation> deliveryOperationList = deliveryOperationService.selectDeliveryOperationList(materialIds,materialName);
        return  getDataTable(deliveryOperationList);
    }



    /**
     * 查询领料出库
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:LingLiao')")
    @GetMapping("/LingLiao")
    public TableDataInfo LingLiao(String shippingType) {
        startPage();
        List<DeliveryOperation> LingLiao = deliveryOperationService.selectLingLiao(shippingType);
        return getDataTable(LingLiao);
    }

    /**
     * 查询退货出库
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:TuiHuo')")
    @GetMapping("/TuiHuo")
    public TableDataInfo TuiHuo(String shippingType) {
        startPage();
        List<DeliveryOperation> TuiHuo = deliveryOperationService.selectTuiHuo(shippingType);
        return getDataTable(TuiHuo);
    }

    /**
     * 查询销售出库
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:XiaoShou')")
    @GetMapping("/XiaoShou")
    public TableDataInfo selectXiaoShou(String shippingType) {
        startPage();
        List<DeliveryOperation> XiaoShou = deliveryOperationService.selectXiaoShou(shippingType);
        return getDataTable(XiaoShou);
    }

    /**
     * 查询外踩出库
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:WaiCai')")
    @GetMapping("/WaiCai")
    public TableDataInfo selectWaiCai(String shippingType) {
        startPage();
        List<DeliveryOperation> QiTa = deliveryOperationService.selectWaiCai(shippingType);
        return getDataTable(QiTa);
    }


//    /**
//     * 新增入库数量
//     */
//    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:edit')")
//    @PutMapping("/into")
//    public AjaxResult updateDeliveryRu(Long delivery_id)
//    {
//        return toAjax(deliveryOperationService.updateDeliveryRu(delivery_id));
//    }

    /**
     * 删除出入库操作
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:remove')")
    @Log(title = "出库操作", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deliveryIds}")
    public AjaxResult remove(@PathVariable Long[] deliveryIds) {
        return toAjax(deliveryOperationService.deleteDeliveryOperationByDeliveryIds(deliveryIds));
    }


    /**
     * 查询质检
     */
    @GetMapping("/findByZhijianList")
    public TableDataInfo findByZhijianList(@RequestParam List<String> zhijian,String libraryType,String shippingType,String materialName,String createUser,String storageStatus) {
        startPage();
        List<DeliveryOperation> deliveryOperations = deliveryOperationService.findByZhijianList(zhijian,libraryType,shippingType,materialName,createUser,storageStatus);

        return getDataTable(deliveryOperations);
    }


    /**
     * 修改质检操作
     */
    @PreAuthorize("@ss.hasPermi('OutboundManagement:deliveryOperation:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody DeliveryOperation deliveryOperation)
    {
        return toAjax(deliveryOperationService.updateDeliveryOperation(deliveryOperation));
    }


    /**
     * 修改出库操作
     */
    @PutMapping("/ChuKu")
    public AjaxResult updateDeliveryChuKu(@RequestBody DeliveryOperation deliveryOperation) {
        return toAjax(deliveryOperationService.updateDeliveryChuKu(deliveryOperation));
    }


    /**
     * 批量修改出库操作
     */
    @PutMapping("/XiuGaiChu")
    public AjaxResult XiuGaiChu(@RequestBody DeliveryOperation deliveryOperation) {
        return toAjax(deliveryOperationService.XiuGaiChu(deliveryOperation));
    }



//    /**
//     * 修改入库数量
//     */
//    @PutMapping("/Ru")
//    public AjaxResult updateDeliveryRu(@RequestBody DeliveryOperation deliveryOperation) {
//        return toAjax(deliveryOperationService.updateDeliveryRu(deliveryOperation));
//    }
//
//
//    /**
//     * 修改出库数量
//     */
//    @PutMapping("/Chu")
//    public AjaxResult updateDeliveryChu(@RequestBody DeliveryOperation deliveryOperation) {
//        return toAjax(deliveryOperationService.updateDeliveryChu(deliveryOperation));
//    }


    @GetMapping("/selectRuTotal")
    public TableDataInfo  selectRuTotal(DeliveryOperation deliveryOperation) {
        List<DeliveryOperation> RuTotal = deliveryOperationService.selectRuTotal(deliveryOperation);
        return getDataTable(RuTotal);
    }


    @GetMapping("/Warning")
    public TableDataInfo selectWarning(WarningVO warningVO)
    {
        startPage();
        List<WarningVO> warning = deliveryOperationService.selectWarning(warningVO);
        return getDataTable(warning);
    }


}
